package cn.kai.org.service.impl;

import cn.kai.basic.service.impl.BaseServiceImpl;
import cn.kai.org.domain.Department;
import cn.kai.org.mapper.DepartmentMapper;
import cn.kai.org.service.IDepartmentServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentServiceI {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 简单写法
     */
//    @Override
//    public List<Department> findDeptTree() {
//        List<Department> all = departmentMapper.findAll();
//        List<Department> result = new ArrayList<>();
//        for (Department dept : all){
//            if (dept.getParentId() == null){
//                result.add(dept);
//            }else {
//                for (Department parent : all){
//                    if (parent.getId() == dept.getParentId()){
//                        List<Department> children = parent.getChildren();
//                        children.add(dept);
//                        break;
//                    }
//                }
//            }
//        }
//        return result;
//    }

    /**
     * 必会写法
     */
    public List<Department> findDeptTree() {
        List<Department> all = departmentMapper.findAll();
        //使用stream流
        Map<Long, Department> parentMap = all.stream().collect(Collectors.toMap(Department::getId, e -> e));
        List<Department> result = new ArrayList<>();
        for (Department dept : all){
            if (dept.getParentId() == null){
                result.add(dept);
            }else {
                Department parent = parentMap.get(dept.getParentId());
                List<Department> children = parent.getChildren();
                children.add(dept);
            }
        }
        return result;
    }



//    public static void main(String[] args) {
//        DepartmentServiceImpl departmentService = new DepartmentServiceImpl();
//        List<Long> integers = departmentService.searchId(2L);
//        integers.forEach(System.out::println);
//    }
//    //创建对象
//    DepartmentServiceImpl departmentService = new DepartmentServiceImpl();
//    List<Department> deptTree = departmentService.findDeptTree();
//    //使用stream流
////  Map<Long, Department> parentMap = deptTree.stream().collect(Collectors.toMap(Department::getId, e -> e));
//    List<Long> ids = new ArrayList<>();
//    //通过父类id查询所有子类id
//    public List<Long> searchId(Long parentId){
//        Department parent = departmentMapper.findById(parentId);
//        if (parent == null){
//            return ids;
//        }
//        List<Department> children = parent.getChildren();
//        if (children != null){
//            for (Department child : children){
//                Long id = child.getId();
//                ids.add(id);
//                departmentService.searchId(id);
//            }
//        }
//        return ids;
//    }
}
